package cn.itsource.org.controller;

import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.Constant;
import cn.itsource.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api("这个增删改查,和登录的接口")
public class DepartmentController {

    /**
     * 注入service层
     */
    @Autowired
    private IDepartmentService iDepartmentService;




    @GetMapping
    @ApiOperation(  value = "默认启动",notes = "查询所有")
    public List<Department> fainAll(){
        return iDepartmentService.fainAll();
    }


    //分页加上,查询展示所有内容,和单个查询
    @PostMapping("/findPage")
    public PageList<Department> findPage(@RequestBody  DepartmentQuery departmentQuery){
        return  iDepartmentService.findPage(departmentQuery);
    }


    /**
     * 通过无限极的方法来得到所有上下级关系
     * @param
     */
    @GetMapping("/Higher")
    public AjaxResult getHigher(){
        try {
            //通过service层中得到已经查询和分配好的数据
            List<Department> higher = iDepartmentService.getHigher();
            //将结果通过ajaxResult中的date属性传回到前端去
            return AjaxResult.success(higher);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.returnFailure(Constant.SYSTEM_EXCEPTION,"返回异常");
        }
    }






    //删除功能
    @DeleteMapping("{id}")
    public AjaxResult Delete(@PathVariable("id") Long id){

        try {
            iDepartmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.returnFailure("删除异常,请找管理员");
        }

    }
    //批量删除功能
    @PostMapping("/batchDeletion")
    public AjaxResult BatchRemvoe(@RequestBody DepartmentQuery departmentQuery){

        try {
            iDepartmentService.BatchDeletion(departmentQuery);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.returnFailure("删除异常,请找管理员");
        }

    }
    //添加和修改功能根据id
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrupdate(@RequestBody @Valid Department department){

        try {
            if (department.getId() == null){
                iDepartmentService.save(department);
            }else{
                iDepartmentService.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.returnFailure(Constant.SYSTEM_EXCEPTION,"返回异常");
        }


    }








}
